package com.sky.controller;

import com.jfinal.aop.Before;
import com.jfinal.log.Log;
import com.sky.intercepter.NotNullPara;
import com.sky.model.User;
import com.sky.validator.LoginValidator;
import io.jboot.web.controller.JbootController;
import io.jboot.web.controller.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : wushikai
 * <p>
 * date : 2020-07-22
 */

@RequestMapping(value = "/login" , viewPath = "/")
public class LoginController extends JbootController {

    Log logger = Log.getLog(LoginController.class);



    public void toLogin() throws InterruptedException {

        HttpServletRequest request = getRequest();
        String flag="1";
        if (Integer.parseInt(flag)==1){
            /*logger.info("我要返回了;");
            return;*/
        }else {


        }
        renderText("wdnmd");
        renderJson("wdnmd");
         redirect("/login/test");  //重定向也是一样, 执行到这里它并不会去跳转页面, 而是直到return 之后才跳转到其他方法;
        //Thread.sleep(3000);
        render("login.html");  //render 执行到这里并没有去跳转页面, 而是在某个地方被 '挡住了' ,
        // 直到这个方法return 之后, 才会去跳转页面; 这个render 直接把redirect 覆盖了
        logger.info("我还在执行");
        return;

    }





    public void test(){

        logger.info("我被调用了");
        User user = new User();
        user.setId(1);
        user.setLoginName("sky");
        renderJson(user);
        return ;
    }





    @NotNullPara({"coupon", "role"})
    @Before(LoginValidator.class)
    public void postLogin(){
        String username = getPara("username");
        String password = getPara("password");
        String coupon = getPara("coupon");
        String role= getPara("role");  //隔了一层方法, 也可以获取从页面上传过来的值
        Map<String, String> map = getMap();
        renderText( "用户名: " + username  + " -- 密码 : "+  password +" 验证码 :  " + coupon + "  角色: " + role + "参数map: "+ map);
    }

    public Map<String, String> getMap(){

        String username = getPara("username");
        String password = getPara("password");
        String coupon = getPara("coupon");
        String role= getPara("role");
        HashMap<String, String> map =new HashMap<>();
        map.put("username", username);


        return  map;
    }



}
